var app = angular.module('myApp', ['ngRoute']);
//定义一个全局变量来控制商品规格的弹框是否出现
var boxFlag = false;
//获取购物车以及购物车里的数量值
var carCar, carVal;
setTimeout(function() {
	carCar = document.getElementsByClassName('r-p1')[0];
	carVal = parseInt(carCar.innerHTML);
}, 200);

//判断是否弹出box弹框,即 选择商品的规格,数量等的弹框

app.config(function($routeProvider) {
		$routeProvider.when('/', {
			templateUrl: 'index.html'
		}).when('/index', {
			templateUrl: 'index.html'
		}).when('/sameCity', {
			templateUrl: 'sameCity.html'
		}).when('/friday', {
			templateUrl: 'friday.html'
		}).when('/jifen', {
			templateUrl: 'jifen.html'
		}).when('/jifen/:id', {
			templateUrl: 'jifen.html'
		}).when('/sameCity/:str', {
			templateUrl: 'sameCity.html'
		}).when('/sameCity2/:id', {
			templateUrl: 'store.html'
		}).when('/jia', {
			templateUrl: 'index.html'
		}).when('/kind', {
			templateUrl: 'kind.html'
		}).when('/product/:id', {
			templateUrl: 'product-detail.html'
		}).when('/jifen/:id', {
			templateUrl: 'product-detail.html'
		}).when('/kind/:id', {
			templateUrl: 'product-detail.html'
		}).when('/dingdan', {
			templateUrl: 'wodedingdan.html'
		}).otherwise({
			templateUrl: 'index.html'
		})

	})

	
//	路由监听
//创建服务
app.service('navTab', function() {
	//导航栏css样式变化
	this.navChange = function(url) {
		var loc = url.path().split('/')[1];
		if(loc == 'index' || loc == '/') {
			$(".header-nav li").removeClass('active');
			$(".header-nav li").eq(1).addClass('active');
		} else if(loc == 'sameCity' || loc == 'str') {
			$(".header-nav li").removeClass('active');
			$(".header-nav li").eq(2).addClass('active');
		} else if(loc == 'friday' || loc == 'm') {
			$(".header-nav li").removeClass('active');
			$(".header-nav li").eq(3).addClass('active');
		} else if(loc == 'jifen') {
			$(".header-nav li").removeClass('active');
			$(".header-nav li").eq(4).addClass('active');
		} else if(loc == 'jia' || loc == 'fs') {
			$(".header-nav li").removeClass('active');
			$(".header-nav li").eq(5).addClass('active');
		}
	}
});
//实时监听路由变化
app.run(['$rootScope', '$location', 'navTab', function($rootScope, $location, navTab) {
	/* 监听路由的状态变化 */
	$rootScope.$on('$routeChangeSuccess', function() {
		console.log('route have already changed ：' + $location.path());
		navTab.navChange($location);
	});
}])

/***************************************首页********************************* */
app.controller('indexCtrl', function($scope, $http) {
	$http.get('../php/index.php').success(function(data) {
		$scope.totalArr = data;
		//定义一个数组存储 商品的类型
		$scope.kinds = [];
		for(var i = 0; i < data.length; i++) {
			//			console.log(data[i].kind);
			if($.inArray(data[i].kind, $scope.kinds) == -1) {
				$scope.kinds.push(data[i].kind)
			}
		}
		console.log($scope.kinds); //$scope.kinds=["meat", "fruit", "sea", "milk", "photo"]

		//拿到商品分类(kind)以后,要根据kind类型进行筛选
		$scope.goods1 = []
		for(var i = 0; i < $scope.kinds.length; i++) {
			$http.get('../php/kind.php?kind=' + $scope.kinds[i]).success(function(data) {
				//拿到的是一个以kind种类区分的数组
				$scope.goods1.push(data);

			})
		}
		console.log($scope.goods1);
	})

	//轮播图
	//点击右键图片开始滚动
	var index = 0;
	var right = document.querySelector('.lb-right');
	var contents = document.querySelectorAll('.lb-content')
	right.onclick = function() {
		if(index == 4) {
			index = 0;
		} else {
			index++;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//点击左键图片向左滚动
	var left = document.querySelector('.lb-left');
	left.onclick = function() {
		if(index <= 0) {
			index = 4;
		} else {
			index--;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//创建计时器让大图滚动起来
	var timmer = setInterval(right.onclick, 1000);
	//当鼠标移入的时候大图滚动停止
	var wrap = document.querySelector('.lb-wrap');
	wrap.onmouseenter = function() {
		clearInterval(timmer);
	};
	//当鼠标移出时,开始大图滚动
	wrap.onmouseleave = function() {
		timmer = setInterval(right.onclick, 1000);
	};
	//添加下边的小圆函数
	function ball(a) {
		for(var j = 0; j < lis.length; j++) {
			if(j == a) {
				lis[j].style.backgroundColor = 'green';
				lis[j].style.opacity = 1;
			} else {
				lis[j].style.backgroundColor = '#000';
				lis[j].style.opacity = 0.2;
			}

		}
	}

	var lis = document.querySelectorAll('.lb-page li');
	lis[0].style.backgroundColor = 'green';
	for(var j = 0; j < lis.length; j++) {
		lis[j].onmouseenter = function() {
			for(var k = 0; k < contents.length; k++) {
				if(this == lis[k]) {
					contents[k].style.display = 'block';
					index = k;
					ball(k);
				} else {
					contents[k].style.display = 'none';
				}
			}

		}
	}

	//	礼拜五商品列表轮播图

	var mySwiper = new Swiper('.swiper-container', {
		direction: 'horizontal',
		loop: true,
		// 如果需要分页器
		pagination: '.swiper-pagination',
		//点击swiper-pagination切换图片
		paginationClickable: true,
		//自动播放.多长时间播放下一张图
		autoplay: 3000,

		//用户操作swiper之后，是否禁止autoplay。默认为true：停止
		autoplayDisableOnInteraction: false,

		//grabCursor 设置为true时，鼠标覆盖Swiper时指针会变成手掌形状，拖动时指针会变成抓手形状。（根据浏览器形状有所不同.
		grabCursor: true,
		//把一张完整的图换掉需要的时间
		speed: 500,
		//动画效果(cube fade overflow flip)
		effect: 'slide',
		//调函数，swiper从一个slide过渡到另一个slide结束时执行。
		onSlideChangeEnd: function(swiper) {
			//console.log(swiper.activeIndex) //切换结束时，告诉我现在是第几个slide
		}
	});
	//	
	//	

	var time = setInterval(function() {
		var days, hour, minute, second;
		//获取2017年6月30日14时0:00;
		var tomorrow = new Date(2017, 5, 30, 14, 0, 0);
		var now = new Date();
		var res1 = tomorrow - now;
		//				var days = Math.floor(res1/1000/60)
		var days = Math.floor(res1 / 86400000);
		var res2 = res1 - days * 86400000;
		hour = Math.floor(res2 / 1000 / 60 / 60);
		var res3 = res2 - hour * 3600000;
		minute = Math.floor(res3 / 1000 / 60 % 60);
		var res4 = res3 - minute * 60000
		second = Math.floor(res4 / 1000);
		////			console.log(days,hour,minute,second);
		var day = days + '天';
		$('.kind-down span').eq(1).html(day);
		$('.kind-down span').eq(2).html(hour);
		$('.kind-down span').eq(3).html(minute);
		$('.kind-down span').eq(4).html(second);
	}, 1000);
})

//导航菜单操作nav
app.controller('navCtrl', function() {
	//点击导航按钮触发事件
	var navs = [];
	for(var i = 1; i < $('.header-nav li').length; i++) {
		navs.push($('.header-nav li').eq(i));
	}
	$.each(navs, function(i, e) {
		$(e).click(function() {
			$('.header-nav li').removeClass('active');
			$(this).addClass('active');
		})
	});
	/*鼠标移到全部分类的时候出现隐藏菜单*/
	$('.all-kinds').mouseenter(function() {
		$('.nav-menu').show();
	}).mouseleave(function() {
		$('.nav-menu').hide();
	})

	$('.nav-menu').mouseenter(function() {
		$(this).show();
	})

	//鼠标移开.item-wrap,隐藏	
	$('.item-wrap').mouseleave(function() {
		$('.item-three').hide();
		$('.nav-menu').hide();
	})
	//鼠标在左侧边栏移动,更换右边子菜单
	console.log($('.nav-menu li'));
	$('.nav-menu li').each(function(i, e) {

		$(e).mouseenter(function() {
			$('.item-three').hide();
			$('.item-three').show(10);
		})
	})

})

/********************************同城服务******************************************/
//轮播图切记一定要第一个引JQ!!!!!!!!!!!!!

app.controller('sameCity', function($scope, $http, $routeParams, $interval) {

	//	console.log($routeParams.str);
	//	var timer;
	//	var index = 0;
	//
	//	function time() {
	//		timer = $interval(function() {
	//			if (index == 5) {
	//				index = 0;
	//			}
	//			$(".r-one-big").css({
	//				left: -$(".r-one-wrap").width() * index
	//			})
	//			$(".r-one-li").removeClass("active");
	//			$(".r-one-li").eq(index).addClass("active");
	//			index++
	//
	//		}, 1500);
	//	};
	//	time();
	//	//鼠标移入移出
	//	$(".r-one-wrap").mouseover(function() {
	//		clearInterval(timer);
	//	})
	//	$(".r-one-wrap").mouseout(function() {
	//			time()
	//		})
	//		//鼠标点击小球
	//	$(".r-one-li").each(function(i, e) {
	//		$(e).click(function() {
	//			clearInterval(timer);
	//			$(".r-one-big").css({
	//				left: -$(".r-one-wrap").width() * i
	//			})
	//			$(".r-one-li").removeClass("active");
	//			$(".r-one-li").eq(i).addClass("active");
	//		})
	//	})

	//点击右键图片开始滚动
	var index = 0;
	var right = document.querySelector('.lb-right');
	var contents = document.querySelectorAll('.lb-content')
	right.onclick = function() {
		if(index == 4) {
			index = 0;
		} else {
			index++;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//点击左键图片向左滚动
	var left = document.querySelector('.lb-left');
	left.onclick = function() {
		if(index <= 0) {
			index = 4;
		} else {
			index--;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//创建计时器让大图滚动起来
	var timmer = setInterval(right.onclick, 1000);
	//当鼠标移入的时候大图滚动停止
	var wrap = document.querySelector('.lb-wrap');
	wrap.onmouseenter = function() {
		clearInterval(timmer);
	};
	//当鼠标移出时,开始大图滚动
	wrap.onmouseleave = function() {
		timmer = setInterval(right.onclick, 1000);
	};
	//添加下边的小圆函数
	function ball(a) {
		for(var j = 0; j < lis.length; j++) {
			if(j == a) {
				lis[j].style.backgroundColor = 'green';
				lis[j].style.opacity = 1;
			} else {
				lis[j].style.backgroundColor = '#000';
				lis[j].style.opacity = 0.2;
			}

		}
	}

	var lis = document.querySelectorAll('.lb-page li');
	lis[0].style.backgroundColor = 'green';
	for(var j = 0; j < lis.length; j++) {
		lis[j].onmouseenter = function() {
			for(var k = 0; k < contents.length; k++) {
				if(this == lis[k]) {
					contents[k].style.display = 'block';
					index = k;
					ball(k);
				} else {
					contents[k].style.display = 'none';
				}
			}

		}
	}

	//点击评分,按销量先后顺序排列
	$(".r-same-button").click(function() {
		$http.get('../php/sales.php').success(function(data) {
			fenye(data);
		})

	})
	////点击销量,按销量先后顺序排列
	$(".r-same-button1").click(function() {
		$http.get('../php/sales1	.php').success(function(data) {
			fenye(data);
		})

	})
	//选中价格从高到低
	$scope.sales = 0;
	$('.r-same-select').change(function() {
		$scope.sales = $('.r-same-select option:selected').text()
		console.log($scope.sales);
		$http.get('../php/price.php?sales=' + $scope.sales).success(function(data) {
			fenye(data);
		})

	})

	//选中价格从低到高
	//点击搜索时获取到的内容
	//接收跳转过来的str内容
	var searchStr = $routeParams.str;
	console.log(searchStr);
	//如果输入框里的值存在就获取,不存在就获取
	if(searchStr) {
		$http.get('../php/search.php?s=' + searchStr).success(function(data) {
//			$scope.aa = data;
				fenye(data);
			//      	console.log(	$scope.aa);
		});
	} else {
		$http.get('../php/sameCity1.php').success(function(data) {
//			$scope.aa = data;
				fenye(data);
		})
	}
	//星星评分
	$scope.starBox = [1, 2, 3, 4, 5];
	//点击获取不同数据
	//	$scope.bb = function(str) {
	//
	//		if (str == "全部") {
	//
	//			$http.get('../php/sameCity1.php').success(function(data) {
	//				$scope.aa = data;
	//			});
	//		} else {
	//			$http.get('../php/search.php?s=' + str).success(function(data) {
	//				$scope.aa = data;
	//				//	        	console.log(	$scope.aa);     
	//			});
	//		}
	//	};

	//点击导航列表,背景变绿,字体变白
	$(".r-one-line a").each(function(inde, ele) {
		$(ele).click(function() {
			$(".r-one-line a").removeClass("r-active")
			$(".r-one-line a").eq(inde).addClass("r-active");
		});
	});
	//新品,热卖
	$http.get('../php/sameCity2.php').success(function(data) {
		$scope.hotData = data.slice(0, 4);
		//      	console.log(	$scope.hotData);

	});

	//点击其他页面清除轮播图的计时器
	//	$scope.$on('$destroy', function() {
	//		$interval.cancel(timer);
	//	});

	//底部分页,点击事件
	//定义每页的数量
	$scope.num = 8;
	$scope.pageArr = [];
	$scope.ind = 0; //当前显示的是第几页
	$scope.pageOn = 0; //当前第几页变量,默认是第一页
	//刷新页面时默认
//	$http.get('../php/sameCity1.php').success(function(data) {
////		fenye(data);
//	});

	//点击导航事件
	$scope.bb = function(str) {
		if(str == "全部") {
			$http.get('../php/sameCity1.php').success(function(data) {
				fenye(data);
			});
		} else {
			$http.get('../php/search.php?s=' + str).success(function(data) {
				fenye(data);

			});
		};

	};

	function fenye(data) {
		//分页
		$scope.all = data;
		$scope.aa = data.slice(0, 8);
		//					$scope.goodsAll = data.slice(0, 8);
		//获取分页数量
		$scope.pageOn = 0;
		$scope.pageArr = [];
		$scope.page = Math.ceil($scope.all.length / 8);
		for(var i = 1; i <= $scope.page; i++) {
			$scope.pageArr.push(i);
		};
	};
	//设置当前点击页
	$scope.setPage = function(i) {
		$(".kind-page-num span").removeClass("active");
		$(".kind-page-num span").eq(i).addClass("active");
		$scope.pageOn = i;
		$scope.aa = $scope.all.slice(i * 8, 8 + 8 * i);
	};

	//点击分页的左右键
	//分页按钮事件
	$scope.numchange = function(n) {
		if(n <= 0) {
			n = 0;
		} else if(n > $scope.page - 1) {
			if(confirm('已经没有更多数据了，为您跳转到最后一页')) {
				n = $scope.page - 1;
				$scope.fsipt = '';
			} else {
				return;
			}
		};
		$scope.ind = n;
		$(".kind-page-num span").removeClass("active");
		$(".kind-page-num span").eq(n).addClass("active");
		$scope.aa = $scope.all.slice(8 * n, 8 * n + 8);
	}

});

/*********************************积分商城******************************************/
app.controller('jifenCtrl', function($scope, $http) {
	//轮播图
	//点击右键图片开始滚动
	var index = 0;
	var right = document.querySelector('.lb-right');
	var contents = document.querySelectorAll('.lb-content')
	right.onclick = function() {
		if(index == 4) {
			index = 0;
		} else {
			index++;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//点击左键图片向左滚动
	var left = document.querySelector('.lb-left');
	left.onclick = function() {
		if(index <= 0) {
			index = 4;
		} else {
			index--;
		}
		for(var i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}
		contents[index].style.display = 'block';
		ball(index);
	}
	//创建计时器让大图滚动起来
	var timmer = setInterval(right.onclick, 1000);
	//当鼠标移入的时候大图滚动停止
	var wrap = document.querySelector('.lb-wrap');
	wrap.onmouseenter = function() {
		clearInterval(timmer);
	};
	//当鼠标移出时,开始大图滚动
	wrap.onmouseleave = function() {
		timmer = setInterval(right.onclick, 1000);
	};
	//添加下边的小圆函数
	function ball(a) {
		for(var j = 0; j < lis.length; j++) {
			if(j == a) {
				lis[j].style.backgroundColor = 'green';
				lis[j].style.opacity = 1;
			} else {
				lis[j].style.backgroundColor = '#000';
				lis[j].style.opacity = 0.2;
			}

		}
	}

	var lis = document.querySelectorAll('.lb-page li');
	lis[0].style.backgroundColor = 'green';
	for(var j = 0; j < lis.length; j++) {
		lis[j].onmouseenter = function() {
			for(var k = 0; k < contents.length; k++) {
				if(this == lis[k]) {
					contents[k].style.display = 'block';
					index = k;
					ball(k);
				} else {
					contents[k].style.display = 'none';
				}
			}
		}
	}

	//获取goods里的数据
	$http.get('../php/index.php').success(function(data) {
		$scope.store = data.slice(5, 9);
	})
})

//商品分类页kind
app.controller('kindCtrl', function($scope, $http) {
	//定义每页的数量
	$scope.num = 8;
	$scope.pageArr = [];
	$scope.goosAll2 = [];
	$scope.ind = 0;
	$scope.pageOn = 0; //当前第几页变量,默认是第一页
	//发送数据请求
	$http.get('../php/main.php').success(function(data) {
		$scope.all = data;
		$scope.goodsAll = data.slice(0, 8);
		$scope.goosAll2 = data;
		console.log($scope.goosAll2);
		//获取分页数量
		$scope.page = Math.ceil($scope.all.length / 8);
		for(var i = 1; i <= $scope.page; i++) {
			$scope.pageArr.push(i);
		}

	})
	//设置当前点击页
	$scope.setPage = function(i) {
		$(".kind-page-num span").removeClass("active");
		$(".kind-page-num span").eq(i).addClass("active");
		$scope.pageOn = i;
		$scope.goodsAll = $scope.all.slice(i * 8, 8 + 8 * i);
	}

	$scope.numchange = function(n) {
		if(n <= 0) {
			n = 0;
		} else if(n > $scope.page - 1) {
			if(confirm('已经没有更多数据了，为您跳转到最后一页')) {
				n = $scope.page - 1;
				$scope.fsipt = '';
			} else {
				return;
			}
		};
		$scope.ind = n;
		$(".kind-page-num span").removeClass("active");
		$(".kind-page-num span").eq(n).addClass("active");
		$scope.goodsAll = $scope.all.slice(8 * n, 8 * n + 8);
	}

	//一级,二级分类
	//声明变量存储一级分类的5个种类
	$scope.fKinds = ['全部', 'fruit', 'sea', 'meat', 'milk'];
	//	$.each($scope.fKinds,function(i,e){
	//		$(e).click(function(){
	//			$()
	//		})
	//	})

	//选择排序方式
	$('.kind-sort-left p').each(function(i, e) {
		$(e).click(function() {
			$('.kind-sort-left p').removeClass('active');
			$(this).addClass('active');
		})
	})

	$('.menu-nn').each(function(index, e) {
		$(e).click(function() {
			$('.menu-nn').removeClass('active');
			$('.kind-menu-s').show();
			$(this).addClass('active');
			if(index == 0) {
				$http.get('../php/main.php').success(function(data) {
					$scope.all = data;
					$scope.goodsAll = data.slice(0, 8);
					$scope.goosAll2 = data;
					//获取分页数量
					$scope.pageOn = 0;
					$scope.pageArr = [];
					$scope.page = Math.ceil($scope.all.length / 8);
					for(var i = 1; i <= $scope.page; i++) {
						$scope.pageArr.push(i);
					}
				})

				$('.kind-menu-s').hide();

			} else {
				$http.get('../php/main2.php?kind=' + $scope.fKinds[index]).success(function(data) {
					$scope.all = data;
					$scope.goodsAll = data.slice(0, 8);
					//获取分页数量
					$scope.pageOn = 0;
					$scope.pageArr = [];
					$scope.page = Math.ceil($scope.all.length / 8);
					for(var i = 1; i <= $scope.page; i++) {
						$scope.pageArr.push(i);
					}
				})
			}

		})
	})

	//点击 按销量排序
	//定义一个变量,初始值为0,和每一个商品的价格作比较
	var minP = 0;
	//定义一个数组,存储排序后的新数组
	$scope.salesNum = function() {
		//遍历数组对象中sales
		//		for(var j = 0; j < $scope.goodsAll.length - 1; j++) {
		//			//				console.log(427);
		//			for(var m = 0; m < $scope.goodsAll.length - j - 1; m++) {
		//				if($scope.goodsAll[m].sales < $scope.goodsAll[m + 1].sales) {
		//					var temp = $scope.goodsAll[m];
		//					$scope.goodsAll[m] = $scope.goodsAll[m + 1];
		//					$scope.goodsAll[m + 1] = temp;
		//				}
		//			}
		//		}
		$scope.goodsAll.sort(function(a, b) {
			return a.sales < b.sales;
		})
		console.log($scope.goodsAll);
	}

	//按价格排序
	$scope.jpShow = false;
	$scope.sortImg = function() {
		$scope.jpShow = !$scope.jpShow;
		//默认价格从低到高排序
		if($scope.jpShow == false) {

			for(var j = 0; j < $scope.goodsAll.length - 1; j++) {
				//				console.log(427);
				for(var m = 0; m < $scope.goodsAll.length - j - 1; m++) {
					if($scope.goodsAll[m].newprice > $scope.goodsAll[m + 1].newprice) {
						var temp = $scope.goodsAll[m];
						$scope.goodsAll[m] = $scope.goodsAll[m + 1];
						$scope.goodsAll[m + 1] = temp;
					}
				}
			}
		} else {
			for(var j = 0; j < $scope.goodsAll.length - 1; j++) {
				//				console.log(427);
				for(var m = 0; m < $scope.goodsAll.length - j - 1; m++) {
					if($scope.goodsAll[m].newprice < $scope.goodsAll[m + 1].newprice) {
						var temp = $scope.goodsAll[m];
						$scope.goodsAll[m] = $scope.goodsAll[m + 1];
						$scope.goodsAll[m + 1] = temp;
					}
				}
			}
		}
	}

	//按评分高低排序
	//	$scope.scoreArr = [];
	$scope.zhScore = function() {
		$scope.goodsAll.sort(function(a, b) {
			return a.score < b.score;
		})
	}

	//筛选全国和次日达
	$('.area2').each(function(i, e) {
		$(e).click(function() {
			$('.area2').removeClass('active');
			$(this).addClass('active');
		})
	})

})

//商品详情页
app.controller('proCtrl', function($scope, $routeParams, $http) {
	var index = 0;
	//点击图片放大显示
	$(".r-two-small1").each(function(i, e) {
		$(e).click(function() {
			$('.r-two-big img').attr('src', $(e).attr('src'));
			$('.r-two-big3 img').attr('src', $(e).attr('src'));
			//				console.log($('.r-two-big').attr('src'));
			$(".r-two-small1").removeClass("r-two-one");
			$(".r-two-small1").eq(i).addClass("r-two-one");
		})
	})
	//点击左右按钮
	//点击左按钮
	$(".r-two-jian:eq(0)").click(function() {
		if(index <= 0) {
			index = 3;
		} else {
			index--;
		}
		$(".r-two-small1").removeClass("r-two-one");
		$(".r-two-small1").eq(index).addClass("r-two-one");
		//展示区域和放大区域
		$('.r-two-big img').attr('src', $(".r-two-small1").eq(index).attr('src'));
		$('.r-two-big3 img').attr('src', $(".r-two-small1").eq(index).attr('src'));
	})
	//点击右键
	$(".r-two-jian:eq(1)").click(function() {
		if(index >= 3) {
			index = 0
		} else {
			index++;
		}
		$(".r-two-small1").removeClass("r-two-one");
		$(".r-two-small1").eq(index).addClass("r-two-one");
		//展示区域和放大区域
		$('.r-two-big img').attr('src', $(".r-two-small1").eq(index).attr('src'));
		$('.r-two-big3 img').attr('src', $(".r-two-small1").eq(index).attr('src'));
	});
	//放大镜
	var small = document.getElementsByClassName("r-two-big")[0];
	var big = document.getElementsByClassName("r-two-big3")[0];
	var zoom = document.getElementsByClassName("r-zoom")[0];
	var bigImg = document.getElementsByClassName("r-two-hidden")[0];
	small.onmousemove = function(e) {
		var even = e || event;
		//			console.log($('.r-two-big').offset().left);
		var x = even.clientX - zoom.offsetWidth / 2 - $('.r-two-big').offset().left;
		var y = even.clientY - zoom.offsetHeight / 2 - $('.r-two-big').offset().top;
		//水平方向的left最大临界值
		var maxX = small.clientWidth - zoom.clientWidth;
		if(x < 0) {
			x = 0
		}
		if(x > maxX) {
			x = maxX
		}
		//竖直方向的top最大临界值
		var maxY = small.clientHeight - zoom.clientHeight;
		if(y < 0) {
			y = 0
		}
		if(y > maxY) {
			y = maxY
		}
		zoom.style.left = x + "px";
		zoom.style.top = y + "px";
		console.log(bigImg.offsetWidth);
		var bigImgX = (x / maxX) * (bigImg.offsetWidth - big.clientWidth);
		var bigImgY = (y / maxY) * (bigImg.offsetHeight - big.clientHeight);
		//切记要给小的赋值,不能给bigImg.scrollLeft = bigImgX 这样写
		big.scrollLeft = bigImgX;
		big.scrollTop = bigImgY;
	}
	//为小图区域添加鼠标移入移出事件
	small.onmouseenter = function() {

		zoom.style.display = "block";
		big.style.display = "block";

		//			$('.r-two-big img').each(function(i,e){
		//				if(!$(e).is(':hidden')){
		//					$('.r-two-big3').find('img').attr('src',$(e).attr('src'));
		//				}
		//			});

	}
	small.onmouseleave = function() {
		zoom.style.display = "none";
		big.style.display = "none";
	}
	//右边点击字体变色
	$(".r-two-write a").each(function(inde, ele) {
		$(ele).click(function() {
			$(".r-two-write a").removeClass("r-two-write1");
			$(".r-two-write a").eq(inde).addClass("r-two-write1");
		})
	})
	//选择重量
	$scope.guige = false;
	//判断是否出现box弹框
	$scope.boxShow = false;
	$(".r-two-middle1 a").each(function(inde, ele) {
		$(ele).click(function() {
			$scope.guige = true;
			$scope.boxShow = false;
			$(".r-two-middle1 a").removeClass("r-two-write2");
			$(".r-two-middle1 a").eq(inde).addClass("r-two-write2");
		})
	})
	//购物车加减数量
	var i = 1;
	//点击减号
	$(".r-two-math button").eq(0).click(function() {
		//			console.log(222);
		if(i == 1) return;
		$(".r-two-input").val(--i);
		//		i = oSpan.value-0;
		//		if (i == 1) return;
		//		oSpan.value = --i
		//		console.log(oSpan.innerHTML);
	});
	//点击加号
	$(".r-two-math button").eq(1).click(function() {
		//			console.log(111);
		//	i++;
		$(".r-two-input").val(++i);
		//	  i = oSpan.value-0;
		//		oSpan.value = ++i
	});

	//点击商品详情
	$(".r-two-head1").each(function(index, element) {
		$(element).click(function() {
			$(".r-two-head1").removeClass("r-two-h");
			$(".r-two-head1").eq(index).addClass("r-two-h");
			$(".r-two-details").removeClass("r-two-data");
			$(".r-two-details").eq(index).addClass("r-two-data");
		})
	})

	//数据请求
	$scope.id = $routeParams.id;
	//      console.log($scope.id);
	$http.get('../php/product.php?id=' + $scope.id).success(function(data) {
		$scope.data2 = data;
		//      	console.log($scope.data2);
	})

	//加入购物车
	$scope.proAdd = function() {

		//			console.log(212)
		if($scope.guige == true) {
			carVal++;
			$(carCar).html(carVal);
		} else {
			boxFlag = true;
			$(".r-opacity1").show();
		}
	}

	//立即购买
	//		$scope.proBuy = function(){
	//			
	//		}

	//收藏
	$scope.isActive = false;
	$scope.isCollect = function() {
		console.log(1233)
		$scope.isActive = !$scope.isActive;
		if($scope.isActive == true) {
			$('.collect-success').animate({
				//					display:'block',
				opacity: 1
			}, 200, function() {
				$('.collect-success').animate({
					opacity: 0
				}, 1000, function() {})
			})
		} else {

			$('.collect-cancel').animate({
				//					display:'block',
				opacity: 1
			}, 200, function() {
				$('.collect-cancel').animate({
					opacity: 0
				}, 1000, function() {})
			})

		}
	}
})

/*******************************商家详情(store.html)*******************************************/
app.controller('storeCtrl', function($scope, $http, $routeParams, $timeout) {
	$scope.id = $routeParams.id;

	$http.get('../php/store1.php?id=' + $scope.id).success(function(data) {
		$scope.data2a = data;
	})

	$timeout(function() {
		//打分,判断后台传过来的值
		var score1 = parseFloat($('.score1').eq(0).html())
		var score2 = parseFloat($('.score2').eq(0).html())
		var score3 = parseFloat($('.score3').eq(0).html())
		var score = parseFloat((score1 + score2 + score3) / 3).toFixed(1) + '分'
		//console.log(score);
		$('.total-score').eq(0).html(score);
		//判断进度条的长度
		var w = $('.store-average-score span').eq(0).width();
		$('.store-progress').width(parseFloat(score) / 5 * w);
	}, 200);

	//商品小轮播图
	//鼠标移入事件
	//声明3个数组分别存储下方的四个小图和其对应的四个大图,以及最大图
	//		var small = ["../img/sjia/small.png","../img/sjia/small.png","../img/sjia/small.png","../img/sjia/small.png"];
	//		var  big =  ["../img/sjia/big.png","../img/photo/1.jpg","../img/sjia/big.png","../img/sjia/big.png"];
	//		var xbig = ["../img/sjia/xbig.png","../img/photo/1.jpg","../img/sjia/xbig.png","../img/sjia/xbig.png"];
	var storeIndex = 0;
	$('.store-lb-small img').each(function(i, e) {
		storeIndex = i - 1;
		$(e).click(function() {
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
			//换中等大图
			$(bigImg).eq(0).attr('src', $(this).attr('src'));
			var str = $(bigImg).eq(0).attr('src');
			//换放大镜大图
			$('.store-scale-img').attr('src', str);
		})
	})
	//左右按钮点击效果:声明一个变量存储当前的图片下标
	$('.store-lb-left').click(function() {
		$('.store-lb-right').css('opacity', 1);
		if(storeIndex <= 0) {
			storeIndex = 0;
			$(this).css('opacity', 0.2);
		} else {
			storeIndex--;
			$(this).css('opacity', 1);
		}
		$('.store-lb-small img').eq(storeIndex).siblings().removeClass('active');
		$('.store-lb-small img').eq(storeIndex).addClass('active');
	})
	$('.store-lb-right').click(function() {
		$('.store-lb-left').css('opacity', 1);
		if(storeIndex >= 3) {
			storeIndex = 3;
			$(this).css('opacity', 0.1);
		} else {
			storeIndex++;
			$(this).css('opacity', 1);
		}
		$('.store-lb-small img').eq(storeIndex).siblings().removeClass('active');
		$('.store-lb-small img').eq(storeIndex).addClass('active');
	})

	//放大镜效果

	var small = document.querySelector('.smallDiv');
	var big = document.querySelector('.store-scale');
	var zoom = document.querySelector('.store-room');
	var bigImg = document.querySelector('.store-scale-img');
	small.onmousemove = function(e) {
		var even = e || event;
		var x = even.clientX - zoom.offsetWidth / 2 - $(small).offset().left;
		var y = even.clientY - zoom.offsetHeight / 2 - $(small).offset().top;
		var maxX = small.clientWidth - zoom.clientWidth;
		var maxY = small.clientHeight - zoom.clientHeight;
		//以下四个 i f判断 黄色遮罩 可移动区域
		if(x <= 0) {
			x = 0;
		}
		if(x >= maxX) {
			x = maxX;
		}
		if(y < 0) {
			y = 0;
		}
		if(y >= maxY) {
			y = maxY;
		}
		zoom.style.left = x + 'px';
		zoom.style.top = y + 'px';

		var bigImgX = (x / maxX) * (bigImg.offsetWidth - big.clientWidth);
		var bigImgY = (y / maxY) * (bigImg.clientHeight - big.clientHeight);

		//给 big 区域添加滚动
		big.scrollLeft = bigImgX;
		big.scrollTop = bigImgY;
	}

	//给 small 添加鼠标移入移出事件
	small.onmouseenter = function() {
		zoom.style.display = 'block';
		big.style.display = 'block';
	}
	small.onmouseleave = function() {
		zoom.style.display = 'none';
		big.style.display = 'none';
	}
})